package com.lottoad.android.cronos.http;

import org.apache.http.HttpResponse;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

public class CNHttpManager {
	public static final int HTTP_METHOD_POST = 1;
	public static final int HTTP_METHOD_GET = 2;
	public static final int HTTP_METHOD_DELETE = 3;
	public static final int HTTP_METHOD_PUT = 4;
	private static volatile CNHttpManager httpManager = null;

	public static CNHttpManager getInstance() {
		if (httpManager == null) {
			synchronized (CNHttpManager.class) {
				if (httpManager == null) {
					httpManager = new CNHttpManager();
				}
			}
		}
		return httpManager;
	}

	public CNHttpResponse.SimpleHttpResponse requestData(Context context, int method, String url, String queryString) {
		CNHttpResponse.SimpleHttpResponse simpleResponse = null;
		HttpResponse response = null;

		if (getNetworkStatus(context)) {
			try {
				if (method == 2) {
					response = CNHttpRequest.getClient(url, addStatisticsQuery(context, queryString));
					simpleResponse = CNHttpResponse.getInstance().getResonseResult(response);
					return simpleResponse;
				}

				if (method == 1) {
					response = CNHttpRequest.postClient(url, addStatisticsQuery(context, queryString));
					simpleResponse = CNHttpResponse.getInstance().getResonseResult(response);
					return simpleResponse;
				}

				if (method == 3) {
					response = CNHttpRequest.deleteClient(url, addStatisticsQuery(context, queryString));
					simpleResponse = CNHttpResponse.getInstance().getResonseResult(response);
					return simpleResponse;
				}

				if (method != 4)
					return simpleResponse;

				response = CNHttpRequest.putClient(url, addStatisticsQuery(context, queryString));
				simpleResponse = CNHttpResponse.getInstance().getResonseResult(response);
				return simpleResponse;
			} catch (Exception e) {
				return CNHttpResponse.getInstance().getResponseServerErrorResult();
			}

		} 
//		else {
//			return simpleResponse;
//		}
		
		else {
			if (isNetworkRoaming(context)) {
				return CNHttpResponse.getInstance().getResponseNetworkRoamingResult();
			}

			return CNHttpResponse.getInstance().getResponseNetworkErrorResult();
		}

	}

	public String getAPIServerURL(Context context, int hostId, int urlResourceId) {
		String host = context.getResources().getString(hostId);
		StringBuilder sb = new StringBuilder();
		sb.append(host);
		sb.append(context.getResources().getString(urlResourceId));
		return sb.toString();
	}

	public static boolean getNetworkStatus(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService("connectivity");
		NetworkInfo netInfo_mobile = cm.getNetworkInfo(0);
		NetworkInfo netInfo_wifi = cm.getNetworkInfo(1);

		if ((netInfo_mobile == null) && (netInfo_wifi == null)) {
			return false;
		}

		return (netInfo_mobile.getState() == NetworkInfo.State.CONNECTED)
				|| (netInfo_wifi.getState() == NetworkInfo.State.CONNECTED)
				|| (isWiMAXNetworkConnected(cm));
	}

	public static boolean get3GMobileNetworkStatus(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService("connectivity");
		NetworkInfo netInfo_mobile = cm.getNetworkInfo(0);

		if (netInfo_mobile == null) {
			return false;
		}

		return netInfo_mobile.getState() == NetworkInfo.State.CONNECTED;
	}

	private static boolean isWiMAXNetworkConnected(
			ConnectivityManager connectivityManager) {
		if (Build.VERSION.SDK_INT >= 8) {
			NetworkInfo wimaxNetwork = connectivityManager.getNetworkInfo(6);
			if ((wimaxNetwork != null) && (wimaxNetwork.isConnected()))
				return true;
		}

		return false;
	}

	private static boolean isNetworkRoaming(Context context) {
		TelephonyManager tm = (TelephonyManager) context.getSystemService("phone");
		return tm.isNetworkRoaming();
	}

	private String addStatisticsQuery(Context context, String query) {
		return query;
	}
}